package jdbc;

import gui2.MainWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import schedule.Appointment;
import schedule.AppointmentList;

public class JdbcMenuAdapter {
	private MainWindow frame;
	private JdbcSchedule jdbc;
	
	public JdbcMenuAdapter(MainWindow frame){
		this.frame = frame;
		addMenus();
		jdbc = new JdbcSchedule();
	}
	
	private void addMenus(){
		JMenuBar mbar;
		if(frame.getJMenuBar() != null)
			mbar = frame.getJMenuBar();
		else
			mbar = new JMenuBar();
		
		JMenu jdbcMenu = new JMenu("JDBC");
		JMenuItem saveAll = new JMenuItem("save all");
		JMenuItem saveSelected = new JMenuItem("save selected appointment");
		JMenuItem load = new JMenuItem("load");
		JMenuItem deleteSelected = new JMenuItem("delete selected appointment");
		mbar.add(jdbcMenu,1);
		jdbcMenu.add(saveAll);
		jdbcMenu.add(load);
		jdbcMenu.add(saveSelected);
		jdbcMenu.add(deleteSelected);
		
		saveAll.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				saveAll();
			}
		});
		
		load.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				load();
			}
		});
		
		deleteSelected.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				deleteSelected(frame.getAppointmentPanel().getJlist().getSelectedValue());
			}
		});
		
		saveSelected.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				saveSelected(frame.getAppointmentPanel().getJlist().getSelectedValue());
			}
		});
	}
	
	/**
	 * saves / updates all appointments in the list
	 */
	public void  saveAll(){
		for(Appointment app : frame.getList()){
			try {
				jdbc.save(app);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(frame, e.getMessage());
			}
		}
	}
	
	/**
	 * saves / updates the selected appointment
	 * @param app
	 */
	public void saveSelected(Appointment app){
		try {
			jdbc.save(app);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(frame, e.getMessage());
		}
	}
	
	/**
	 * removes all entries and adds all appointments from the database
	 * @return
	 */
	public void load(){
		try {
//			frame.getAppointmentPanel().getJlist().removeAll();
			frame.getList().retainAll(new AppointmentList()); //removes all Appointments
			frame.getList().addAll(jdbc.selectAll());
			frame.getAppointmentPanel().getJlist().updateUI();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(frame, e.getMessage());
		}
	}
	
	/**
	 * deletes an appointment in the database
	 * @param app
	 */
	public void deleteSelected(Appointment app){
		try{
	
			if(app != null){
				if(jdbc.delete(app) == 0)
					throw new Exception("deleting failed");
				frame.getAppointmentPanel().getJlist().updateUI();
			} else{
				throw new Exception("no appointment selected");
			}
		} catch(Exception e){
			JOptionPane.showMessageDialog(frame, e.getMessage());
		}
	}
}
